package com.xwj.back.service.impl.user;

import com.xwj.back.mapper.UserMapper;
import com.xwj.back.pojo.User;
import com.xwj.back.service.impl.user.detail.UserDetailsImpl;
import com.xwj.back.service.user.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class InfoServiceImpl implements InfoService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String, String> getUserInfo() {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();
        Map<String,String> map = new HashMap<>();
        if(!user.isStatus()){
            map.put("status","stop");
            return map;
        }
        Date date = new Date();
        user.setLoginTime(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String loginTime = dateFormat.format(date);
        userMapper.updateById(user);
        map.put("status","normal");
        map.put("id",user.getId().toString());
        map.put("username",user.getUsername());
        map.put("photo",user.getPhoto());
        map.put("phone",user.getPhone());
        map.put("role",user.getRole());
        map.put("loginTime",loginTime);
        return map;
    }
}
